<template>
  <div
    class="customer-first"
    v-loading="loading"
    element-loading-text="加载中..."
  >
    <div class="customer-first-title">
      <div class="customer-first-title-left">类型设置</div>
      <div class="customer-first-title-right">
        <el-button type="primary" @click="addDeptFun('add')"> 添加 </el-button>
      </div>
    </div>
    <div>
      <el-table
        border
        stripe
        :data="tableData"
        :height="store.tableHeight - 50"
      >
        <el-table-column prop="name" label="分类名称" />
        <el-table-column prop="customer_num" label="客保数量" />
        <el-table-column prop="status" label="是否启用" width="90">
          <template #default="{ row }">
            <el-switch
              v-model="row.status"
              active-text="是"
              :active-value="1"
              inactive-text="否"
              :inactive-value="0"
              inline-prompt
              @change="statusFun(row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="status" label="是否默认分类">
          <template #default="{ row }">
            <el-tag v-if="row.is_default == 1"> 是 </el-tag>
            <el-tag v-else type="warning">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_user" label="操作人" />
        <el-table-column prop="created_at" label="操作时间" />
        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button type="primary" link @click="updateDeptFun(row)">
              编辑
            </el-button>
            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <addDialog ref="addDialogRef" @refreshList="refreshList" />
</template>
<script setup>
import addDialog from "./dialog/DialogFirst.vue";
import { listApi, delApi, stateApi } from "@/plugins/api/customer/setup/first";
import { useCounterStore } from "@/stores/counter.js";
const store = useCounterStore();
let tableData = ref([]);
let loading = ref(false);
function getData() {
  loading.value = true;
  listApi()
    .then((data) => {
      tableData.value = data;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}

function updateDeptFun(row) {
  addDialogRef.value.initFun("update", row);
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
function statusFun(row) {
  stateApi({
    status: row.status,
    id: row.id.toString(),
  }).then(() => {
    ElMessage({
      type: "success",
      message: "修改成功！",
    });
    getData();
  });
}
//增加
let addDialogRef = ref(null);
function addDeptFun() {
  addDialogRef.value.initFun("add");
}
// 刷新
function refreshList() {
  getData();
}
onMounted(() => {
  getData();
});
</script>
<style lang="scss" scoped>
.customer-first-title {
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #eeeeee;
  padding-bottom: 10px;
  .customer-first-title-left {
    align-self: center;
    font-size: 18px;
    font-weight: 700;
    color: #172b4d;
  }
}
</style>
